package wechat.base.snd;

import com.google.common.base.Objects;
import java.net.URLEncoder;
import java.util.Date;
import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.InputOutput;
import org.eclipse.xtext.xbase.lib.Pure;
import org.json.JSONException;
import org.json.JSONObject;
import wechat.base.snd.WCMPNews;
import wechat.base.snd.WCMediaInfo;
import wechat.base.snd.WCMenu;
import wechat.base.snd.WCNews;
import wechat.base.snd.util.ExecGet;
import wechat.base.snd.util.ExecPost;
import wechat.base.snd.util.PostFile;

@SuppressWarnings("all")
public class WCApplication {
  @Accessors
  private String CORP_ID;
  
  @Accessors
  private String SECRET;
  
  @Accessors
  private String AGENT_ID;
  
  private String TOKEN = null;
  
  private String authurl;
  
  public WCApplication(final String corp_id, final String secret, final String agent_id) {
    this.CORP_ID = corp_id;
    this.SECRET = secret;
    this.AGENT_ID = agent_id;
    this.authurl = ((WCApplication.WECHAT_AUTH_URL + this.CORP_ID) + "&redirect_uri=");
  }
  
  private static String WECHAT_PLATFORM_URL = "https://qyapi.weixin.qq.com";
  
  private static Long TOKEN_EXPIRATION_TIME = Long.valueOf((7200 * 1000l));
  
  private static Date tokenTime = new Date();
  
  private static String WECHAT_AUTH_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=";
  
  public String getToken() {
    String _xblockexpression = null;
    {
      if ((((new Date().getTime() - WCApplication.tokenTime.getTime()) >= (WCApplication.TOKEN_EXPIRATION_TIME).longValue()) || Objects.equal(this.TOKEN, null))) {
        try {
          InputOutput.<String>print("**********get token*****");
          final String tokenUrl = ((((WCApplication.WECHAT_PLATFORM_URL + "/cgi-bin/gettoken?corpid=") + this.CORP_ID) + "&corpsecret=") + this.SECRET);
          String _excuteGet = ExecGet.excuteGet(tokenUrl);
          final JSONObject js = new JSONObject(_excuteGet);
          js.getString("access_token");
          String _string = js.getString("access_token");
          this.TOKEN = _string;
          Date _date = new Date();
          WCApplication.tokenTime = _date;
          InputOutput.<String>println(("token=" + this.TOKEN));
        } catch (final Throwable _t) {
          if (_t instanceof Exception) {
            final Exception e = (Exception)_t;
            InputOutput.<String>println("**********Failed to get token*****");
            this.TOKEN = null;
            e.printStackTrace();
          } else {
            throw Exceptions.sneakyThrow(_t);
          }
        }
      }
      _xblockexpression = this.TOKEN;
    }
    return _xblockexpression;
  }
  
  public String getApplicationUrl() {
    String _token = this.getToken();
    return ((WCApplication.WECHAT_PLATFORM_URL + "/cgi-bin/message/send?access_token=") + _token);
  }
  
  public String getApplicationMediaUrl() {
    String _token = this.getToken();
    return ((WCApplication.WECHAT_PLATFORM_URL + "/cgi-bin/media/upload?access_token=") + _token);
  }
  
  public WCNews createNews() {
    return new WCNews(this);
  }
  
  public WCMPNews createMPNews() {
    return new WCMPNews(this);
  }
  
  public WCMediaInfo uploadTmpFile(final String filePath) {
    String _applicationMediaUrl = this.getApplicationMediaUrl();
    String _uploadFile = PostFile.uploadFile(_applicationMediaUrl, filePath, "image");
    return new WCMediaInfo(_uploadFile);
  }
  
  public String deleteMenu() {
    try {
      String _token = this.getToken();
      String _plus = ("https://qyapi.weixin.qq.com/cgi-bin/menu/delete?access_token=" + _token);
      String _plus_1 = (_plus + "&agentid=");
      String _plus_2 = (_plus_1 + this.AGENT_ID);
      return ExecGet.excuteGet(_plus_2);
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public String createMenu(final WCMenu menu) {
    String _token = this.getToken();
    String _plus = ("https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token=" + _token);
    String _plus_1 = (_plus + "&agentid=");
    String _plus_2 = (_plus_1 + this.AGENT_ID);
    JSONObject _build = menu.build();
    String _string = _build.toString();
    return ExecPost.excuteJsonPost(_plus_2, _string);
  }
  
  public String getMenuInfo() {
    try {
      String _xblockexpression = null;
      {
        String _token = this.getToken();
        String _plus = ("https://qyapi.weixin.qq.com/cgi-bin/menu/get?access_token=" + _token);
        String _plus_1 = (_plus + "&agentid=");
        final String url = (_plus_1 + this.AGENT_ID);
        _xblockexpression = ExecGet.excuteGet(url);
      }
      return _xblockexpression;
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public String buildAuthConnection(final String redirectUrl, final String state) {
    try {
      String _xblockexpression = null;
      {
        String _encode = URLEncoder.encode(redirectUrl, "utf-8");
        String _plus = ("URLEncoder::encode(redirectUrl, \"utf-8\"):::" + _encode);
        InputOutput.<String>println(_plus);
        String _encode_1 = URLEncoder.encode(redirectUrl, "utf-8");
        String _plus_1 = (this.authurl + _encode_1);
        String _plus_2 = (_plus_1 + "&response_type=code&scope=snsapi_base");
        String _plus_3 = (_plus_2 + "&agentid=");
        String _plus_4 = (_plus_3 + this.AGENT_ID);
        String _plus_5 = (_plus_4 + "&state=");
        _xblockexpression = (_plus_5 + state);
      }
      return _xblockexpression;
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public String getUserId(final String code) {
    try {
      String _xblockexpression = null;
      {
        InputOutput.<String>println("***********enter in getUserId**************");
        String _token = this.getToken();
        String _plus = ("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=" + _token);
        String _plus_1 = (_plus + "&code=");
        String url = (_plus_1 + code);
        InputOutput.<String>println(("*********url*********" + url));
        String result = ExecGet.excuteGet(url);
        InputOutput.<String>println(("*************UserInfo JSONObject*************" + result));
        String _xtrycatchfinallyexpression = null;
        try {
          String _xblockexpression_1 = null;
          {
            final JSONObject js = new JSONObject(result);
            String _string = null;
            if (js!=null) {
              _string=js.getString("UserId");
            }
            _xblockexpression_1 = _string;
          }
          _xtrycatchfinallyexpression = _xblockexpression_1;
        } catch (final Throwable _t) {
          if (_t instanceof JSONException) {
            final JSONException e = (JSONException)_t;
            Object _xblockexpression_2 = null;
            {
              e.printStackTrace();
              _xblockexpression_2 = null;
            }
            _xtrycatchfinallyexpression = ((String)_xblockexpression_2);
          } else {
            throw Exceptions.sneakyThrow(_t);
          }
        }
        _xblockexpression = _xtrycatchfinallyexpression;
      }
      return _xblockexpression;
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  @Pure
  public String getCORP_ID() {
    return this.CORP_ID;
  }
  
  public void setCORP_ID(final String CORP_ID) {
    this.CORP_ID = CORP_ID;
  }
  
  @Pure
  public String getSECRET() {
    return this.SECRET;
  }
  
  public void setSECRET(final String SECRET) {
    this.SECRET = SECRET;
  }
  
  @Pure
  public String getAGENT_ID() {
    return this.AGENT_ID;
  }
  
  public void setAGENT_ID(final String AGENT_ID) {
    this.AGENT_ID = AGENT_ID;
  }
}
